package cn.iocoder.yudao.module.nginxui.controller.admin.eventsconfig;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nginxui.controller.admin.eventsconfig.vo.*;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.eventsconfig.EventsConfigDO;
import cn.iocoder.yudao.module.nginxui.service.eventsconfig.EventsConfigService;

@Tag(name = "管理后台 - Nginx events块配置")
@RestController
@RequestMapping("/nginxui/events-config")
@Validated
public class EventsConfigController {

    @Resource
    private EventsConfigService eventsConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建Nginx events块配置")
    @PreAuthorize("@ss.hasPermission('nginxui:events-config:create')")
    public CommonResult<Long> createEventsConfig(@Valid @RequestBody EventsConfigSaveReqVO createReqVO) {
        return success(eventsConfigService.createEventsConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Nginx events块配置")
    @PreAuthorize("@ss.hasPermission('nginxui:events-config:update')")
    public CommonResult<Boolean> updateEventsConfig(@Valid @RequestBody EventsConfigSaveReqVO updateReqVO) {
        eventsConfigService.updateEventsConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Nginx events块配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nginxui:events-config:delete')")
    public CommonResult<Boolean> deleteEventsConfig(@RequestParam("id") Long id) {
        eventsConfigService.deleteEventsConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除Nginx events块配置")
                @PreAuthorize("@ss.hasPermission('nginxui:events-config:delete')")
    public CommonResult<Boolean> deleteEventsConfigList(@RequestParam("ids") List<Long> ids) {
        eventsConfigService.deleteEventsConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得Nginx events块配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nginxui:events-config:query')")
    public CommonResult<EventsConfigRespVO> getEventsConfig(@RequestParam("id") Long id) {
        EventsConfigDO eventsConfig = eventsConfigService.getEventsConfig(id);
        return success(BeanUtils.toBean(eventsConfig, EventsConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得Nginx events块配置分页")
    @PreAuthorize("@ss.hasPermission('nginxui:events-config:query')")
    public CommonResult<PageResult<EventsConfigRespVO>> getEventsConfigPage(@Valid EventsConfigPageReqVO pageReqVO) {
        PageResult<EventsConfigDO> pageResult = eventsConfigService.getEventsConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EventsConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Nginx events块配置 Excel")
    @PreAuthorize("@ss.hasPermission('nginxui:events-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEventsConfigExcel(@Valid EventsConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EventsConfigDO> list = eventsConfigService.getEventsConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "Nginx events块配置.xls", "数据", EventsConfigRespVO.class,
                        BeanUtils.toBean(list, EventsConfigRespVO.class));
    }

}